package top.lingkang.finalgateway.core;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
import lombok.extern.slf4j.Slf4j;
import top.lingkang.finalgateway.config.RouterConfig;

/**
 * @author lingkang
 * Created by 2024/5/5
 */
@Slf4j
public class ServerInitHandler extends ChannelInitializer<SocketChannel> {
    private final RouterConfig config;

    public ServerInitHandler(RouterConfig config) {
        this.config = config;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new HttpServerCodec());// http编解码
        pipeline.addLast(new FinalHttpObjectAggregator(config.getMaxContentLength()));
        pipeline.addLast(new DispatcherHandler(config));
    }
}
